module.exports = function ($scope, $resource, $modalInstance, item, toaster) {
    
    $scope.policyInfo = angular.copy(item);
    $scope.obj = {};    //模态框临时存放乱七八糟的数据

    //确认保存
    $scope.ok = function() {
        if($scope.obj.rangeTag){
            //点击过补贴范围页,处理补贴范围数据
            if($scope.obj.type != 0){
                $scope.policyInfo.rangeList = $scope.rangeList;
                $scope.policyInfo.range_limit = '1';
            } else {
                $scope.policyInfo.rangeList = null;
                $scope.policyInfo.range_limit = '0';
            }
        } else {
            //未点击过补贴范围页,删除此属性,
            delete $scope.policyInfo.range_limit;
        }

        if($scope.obj.standardTag){
            //点击过补贴标准页,处理补贴范围数据
            $scope.policyInfo.standardList = $scope.standardList;
        } else {
            //未点击过补贴标准页,删除此属性
            delete $scope.policyInfo.standardList;
        }

        $resource('/api/ac/atc/policyService/savePolicy', {}, {}).save($scope.policyInfo, function(res){
            if (res.errcode === 0) {
                toaster.success({title: '', body: '保存成功'});
                $modalInstance.close($scope.policyInfo);
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }
    
    //关闭
    $scope.close = function () {
        $modalInstance.dismiss('cancel');
    }

    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓以下是---补贴标准---标签页需要的函数↓↓↓↓↓↓↓↓↓↓↓↓
    //查询已规定中已添加的补贴标准
    $scope.getSubsidyStandard = function() {
        $resource('/api/as/atc/policy/getSubsidyList', {}, {}).save({'policy_code': item.code}, function(res){
            if (res.errcode === 0) {
                $scope.standardList = res.data;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //点击补贴标准tab页
    $scope.seeStandard = function(tag) {
        $scope.obj.touristTypeList = [
            {'label': '国内游客', 'value': '1'},
            {'label': '国外游客', 'value': '2'}
        ];
        $scope.obj.standardTag = tag;   //标记,为true表示点击过-补贴范围-标签页
        if(!$scope.standardList){
            $scope.standard = {'tourist_type': '1'};
            $scope.getSubsidyStandard();
        }
        //加载模态框后,第一次点击标签页刷新,之后不刷新
        if(!$scope.obj.subsidyTypeList){
            $resource('/api/as/atc/policy/getSubsidyTypeList', {}, {}).save({}, function(res){
                if (res.errcode === 0) {
                    $scope.obj.subsidyTypeList = res.data;
                } else {
                    toaster.error({title: '', body: res.errmsg});
                }
            });
        }
    }

    //给法规添加补贴标准
    $scope.addStandard = function(obj) {
        if(!obj){
            return ;
        }
        var temp = {
            'subsidy_type_code': obj.type_code,
            'subsidy_type_name': obj.name,
            'subsidy_money': $scope.standard.subsidy_money,
            'lower_limit_person': $scope.standard.lower_limit_person || 0,
            'upper_limit_person': $scope.standard.upper_limit_person,
            'tourist_type': $scope.standard.tourist_type
        }
        if(!checkParam(temp)) return;
        var i = 0, s;
        for(; i < $scope.standardList.length; i++){
            s = $scope.standardList[i];
            if(obj.type_code == s.subsidy_type_code && temp.tourist_type == s.tourist_type){
                if(temp.lower_limit_person == s.lower_limit_person && temp.upper_limit_person == s.upper_limit_person){
                    toaster.warning({title: '', body: '相同的补贴类型无法重复添加'});
                    return ;
                }
                if(temp.lower_limit_person < s.upper_limit_person && 
                    (!temp.upper_limit_person || temp.upper_limit_person > s.lower_limit_person)){
                    toaster.warning({title: '', body: '人数范围相冲突, 无法添加'});
                    return ;
                }
            }
        }

        $scope.standardList.push(temp);
        $scope.standard.subsidy_money = undefined;
        $scope.standard.lower_limit_person = undefined;
        $scope.standard.upper_limit_person = undefined;
    }

    //编辑模态框里删除补贴标准
    $scope.deleteStandard = function(index) {
        $scope.standardList.splice(index, 1);
    }

    //↓↓↓↓↓↓↓↓↓↓↓↓↓↓以下是---补贴范围---标签页需要的函数↓↓↓↓↓↓↓↓↓↓↓↓
    //查询已规定中已添加的补贴标准
    $scope.getSubsidyRange = function() {
        $resource('/api/as/atc/policy/getSubsidyRangeList', {}, {}).save({'policy_code': item.code}, function(res){
            if (res.errcode === 0) {
                $scope.rangeList = res.data;
                if(res.data.length == 0){
                    $scope.obj.type = '0';
                } else {
                    $scope.obj.type = item.range_limit == '0' ? '0' : res.data[0].type;
                }
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    //点击补贴范围标签页执行
    $scope.seeSubsidyRange = function(tag) {
        $scope.obj.contain = '1';
        $scope.obj.rangeTag = tag;   //标记,为true表示点击过-补贴范围-标签页
        if(!$scope.rangeList){
            $scope.getSubsidyRange();
        }
    }

    //切换限制条件清空条件列表
    $scope.setType = function() {
        $scope.rangeList = [];
    }
    
    //给法规添加补贴标准
    $scope.addSubsidyRange = function() {
        if(!$scope.obj.context){
            return;
        }
        for(var i = 0; i < $scope.rangeList.length; i++){
            if($scope.rangeList[i].context == $scope.obj.context){
                toaster.warning({title: '', body: '无法重复添加'});
                return;
            }
        }
        var temp = {
            'id': (new Date()).valueOf() + '',
            'contain': $scope.obj.contain,
            'type': $scope.obj.type,
            'context': $scope.obj.context
        }
        $scope.rangeList.push(temp);
        $scope.obj.context = undefined;
    }

    //编辑模态框里删除补贴限制
    $scope.removeRange = function(id) {
        for(i = 0; i < $scope.rangeList.length; i++){
            if($scope.rangeList[i].id == id){
                $scope.rangeList.splice(i, 1);
            }
        }
    }
        


    //添加或修改补贴类型是检验数据合法性
	function checkParam(item) {
		if(!item.subsidy_type_code){
			toaster.warning({'title': '', 'body': '请选择补贴类型'});
			return false;
		}
		if(!item.subsidy_money || item.subsidy_money < 0){
			toaster.warning({'title': '', 'body': '补贴金额错误'});
			return false;
		}
		if(item.lower_limit_person || item.lower_limit_person >= 0){ } else {
            toaster.warning({'title': '', 'body': '人数下限错误'});
			return false;
        }
		if(!item.upper_limit_person){
            item.upper_limit_person = undefined;
        } else if(item.upper_limit_person <= item.lower_limit_person){
            toaster.warning({'title': '', 'body': '人数上限不能少于人数下限'});
			return false;
        }
		return true;
	}

};